<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Docker入门-docker compose的使用</h3>
            
            <small class="text-muted">发布于 2019-08-19</small>
        </div>

        <article>
            <h2 id="compose简介">Compose简介</h2>
<p>Compose项目是Docker官方的开源项目，负责实现对Docker容器集群的快速编排。其代码目前在https://github.com/docker/compose 上开源。</p>
<p>Compose定位是定义和运行多个Docker容器的应用，其前身是开源项目Fig。</p>
<p>通过前面内容的介绍，我们知道使用一个Dockerfile模板文件，可以让用户很方便的定义一个单独的应用容器。然而，在日常工作中，经常会碰到需要多个容器相互配合来完成某任务的情况。例如要实现一个Web项目,除了Web服务容器本身，往往还需要加上后端的数据库服务容器，甚至还包括负载均衡容器等。</p>
<p>Compose恰好满足了这样的需求。它允许用户通过一个单独的docker-compose.yml模板文件来定义一组相关联的应用容器为一个项目(project)。</p>
<p>Compose中有两个重要的概念：</p>
<ul>
<li>服务(service):一个应用的容器，实际上可以包括若干运行相同镜像的容器实例。</li>
<li>项目(project):由一组关联的应用容器组成的一个完整业务单元。</li>
</ul>
<p>Compose的默认管理对象是项目，通过子命令对项目中的一组容器进行便捷地生命周期管理。</p>
<p>Compose项目由Python编写，实现上调用了Docker服务提供的API来对容器进行管理。</p>
<h2 id="安装和卸载">安装和卸载</h2>
<p>Compose支持Linux、macOS、Windows10三大平台。</p>
<p>Compose可以通过Python的包管理工具pip进行安装，也可以直接下载编译好的二进制文件使用，甚至能够直接在Docker容器中运行。</p>
<p>Docker for Mac、Docker for Windows自带docker-compose二进制文件，安装Docker之后可以直接使用。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker-compose --version
</span></span></code></pre></div><p>Linux系统需要单独使用二进制或者pip方式进行安装。</p>
<h3 id="linux安装docker-compose">Linux安装docker-compose</h3>
<h4 id="二进制包">二进制包</h4>
<p>在Linux上的安装十分简单，从官方GitHub Release处直接下载编译好的二进制文件即可。例如，在Linux64位系统上直接下载对应的二进制包。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo curl -L <span style="color:#e6db74">&#34;https://github.com/docker/compose/releases/download/1.24.1/docker-compose-</span><span style="color:#66d9ef">$(</span>uname -s<span style="color:#66d9ef">)</span><span style="color:#e6db74">-</span><span style="color:#66d9ef">$(</span>uname -m<span style="color:#66d9ef">)</span><span style="color:#e6db74">&#34;</span> -o /usr/local/bin/docker-compose
</span></span><span style="display:flex;"><span>sudo chmod +x /usr/local/bin/docker-compose  <span style="color:#75715e">#赋予可执行权限</span>
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-01.png" alt=""></p>
<h4 id="pip安装">PIP安装</h4>
<p>如果您计算机的架构是ARM(例如，树莓派),建议使用pip安装。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo pip install -U docker-compose
</span></span></code></pre></div><h2 id="使用">使用</h2>
<p>场景：最常见的项目是web网站，一般的web网站都会依赖第三方提供的服务(比如：DB和cache),我们拿dubbo-admin进行讲解(dubbo-admin依赖zookeeper)。</p>
<h3 id="compose构建dubbo-admin服务">Compose构建dubbo-admin服务</h3>
<p>从github上获取dubbo-admin的master分支源码</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git clone -b master https://github.com/apache/incubator-dubbo-ops.git
</span></span></code></pre></div><p>修改admin中的application配置，把zookeeper地址改为zookeeper://zookeeper:2181</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-02.png" alt=""></p>
<p>使用maven进行编译打包</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>mvn clean package -Dmaven.test.skip<span style="color:#f92672">=</span>true
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-03.png" alt=""></p>
<p>在dubbo-admin目录下编写Dockerfile文件，内容为</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># FROM,表示使用JDK8环境为基础镜像，如果镜像不是本地会从DockerHub进行下载</span>
</span></span><span style="display:flex;"><span>FROM openjdk:8-jdk-alpine
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 作者</span>
</span></span><span style="display:flex;"><span>MAINTAINER Simon&lt;xueyao.me@gmail.com&gt;
</span></span><span style="display:flex;"><span>VOLUME /tmp
</span></span><span style="display:flex;"><span><span style="color:#75715e"># ADD,拷贝文件并且重命名</span>
</span></span><span style="display:flex;"><span>ADD ./target/dubbo-admin-0.0.1-SNAPSHOT.jar app.jar
</span></span><span style="display:flex;"><span><span style="color:#75715e"># ENTRYPOINT,为了缩短Tomcat启动时间，添加java.security.egd的系统属性指向/dev/urandom作为ENTRYPOINT</span>
</span></span><span style="display:flex;"><span>ENTRYPOINT <span style="color:#f92672">[</span><span style="color:#e6db74">&#34;java&#34;</span>, <span style="color:#e6db74">&#34;-Djava.security.egd=file:/dev/./urandom&#34;</span>,<span style="color:#e6db74">&#34;-jar&#34;</span>,<span style="color:#e6db74">&#34;/app.jar&#34;</span><span style="color:#f92672">]</span>
</span></span></code></pre></div><p>使用docker build -t dubbo-admin:1.0 .命令进行构建。
<img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-04.png" alt=""></p>
<p>在项目根目录下编写docker-compose.yml文件，这个是Compose使用的主模板文件。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yml" data-lang="yml"><span style="display:flex;"><span><span style="color:#f92672">version</span>: <span style="color:#e6db74">&#39;3.4&#39;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">services</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">zk_server</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">zookeeper:3.4</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">2181</span>:<span style="color:#ae81ff">2181</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">dubbo-admin</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">dubbo-admin:1.0</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">links</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">zk_server:zookeeper</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">depends_on</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">zk_server</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">7001</span>:<span style="color:#ae81ff">7001</span> 
</span></span></code></pre></div><p>在docker-compose.yml文件所在目录执行：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker-compose up
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-05.png" alt=""></p>
<p>在浏览器中访问http://服务器ip:7001 进行验证，用户名密码为:root/root guest/guest</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-compose-06.png" alt=""></p>
<h2 id="compose命令说明">Compose命令说明</h2>
<h3 id="命令对象与格式">命令对象与格式</h3>
<p>执行docker-compose [COMMAND] &ndash;help或者docker-compose help [COMMAND]可以查看具体某个命令的使用格式。</p>
<p>docker-compose命令的基本的使用格式是：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker-compose <span style="color:#f92672">[</span>-f<span style="color:#f92672">=</span>&lt;arg&gt;...<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>options<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>COMMAND<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>ARGS...<span style="color:#f92672">]</span>
</span></span></code></pre></div><p>命令选项</p>
<ul>
<li>-f,&ndash;file FILE指定模板文件，默认为docker-compose.yml,可以多次指定。</li>
<li>-p,&ndash;project-name NAME指定项目名称，默认将使用所在目录名称作为项目名。</li>
<li>&ndash;x-networking使用Docker的可拔插网络后端特性</li>
<li>&ndash;x-network-driver DRIVER指定网络后端的驱动,默认为bridge</li>
<li>&ndash;verbose输出更多调试信息。</li>
<li>-v,&ndash;version打印版本并退出。</li>
</ul>
<p>build</p>
<pre tabindex="0"><code>格式为docker-compose build [options] [SERVICE...]。
构建(重新构建)项目中的服务容器。
可以随时在项目目录下运行docker-compose build来重新构建服务。
选项包括：
* --force-rm 删除构建过程中的临时容器。
* --no-cache 构建镜像过程中不使用cache(将加长构建过程)。
* --pull 始终尝试通过pull来获取更新版本的镜像。
</code></pre><p>logs</p>
<pre tabindex="0"><code>格式为docker-compose logs [options] [SERVICE...]。
查看服务容器的输出。默认情况下,docker-compose将对不同的服务输出使用不同的颜色来区分。可以通过--no-color来关闭颜色。
</code></pre><p>port</p>
<pre tabindex="0"><code>格式为docker-compose port [options] SERVICE PRIVATE_PORT。
打印某个容器端口所映射的公共端口。
选项：
 --protocol=proto指定端口协议，tcp(默认值)或者udp。
 --index=index如果同一服务存在多个容器，指定命令对象容器的序号(默认为1)。
</code></pre><p>ps</p>
<pre tabindex="0"><code>格式为docker-compose ps [options] [SERVICE...]
列出项目中目前的所有容器。
选项：
 -q只打印容器的ID信息。
</code></pre><p>pull</p>
<pre tabindex="0"><code>格式为docker-compose pull [options] [SERVICE...]。
拉取服务依赖的镜像。
选项：
 --ignore-pull-failures忽略拉取镜像过程中的错误。
</code></pre><p>restart</p>
<pre tabindex="0"><code>格式为docker-compose restart [options] [SERVICE...]
重启项目中的服务。
选项：
 -t,--timeout TIMEOUT指定重启前停止容器的超时(默认为10秒)。
</code></pre><p>rm</p>
<pre tabindex="0"><code>格式为docker-compose rm [options] [SERVICE...]
删除所有(停止状态的)服务容器。推荐先执行docker-compose stop命令来停止容器。
选项：
 -f,--force强制直接删除，包括非停止状态的容器。一般尽量不要使用该选项。
 -v删除容器所挂载的数据卷。
</code></pre><p>run</p>
<pre tabindex="0"><code>格式为docker-compose run[options] [-p PORT...][-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
在指定服务上执行一个命令。例如：
docker-compose run ubuntu ping docker.com
</code></pre><p>scale</p>
<pre tabindex="0"><code>格式为docker-compose scal [options] [SERVICE=NUM...]
设置指定服务运行的容器个数。例如：
docker-compose scale web=3 db=2
将启动3个容器运行web服务，2个容器运行db服务。
</code></pre><p>up</p>
<pre tabindex="0"><code>该命令十分强大，它将尝试自动完成包括构建镜像，(重新)创建服务,启动服务，并关联服务相关容器的一系列操作。链接的服务都将会被自动启动，除非已经处于运行状态。
选项：
 -d 在后台运行服务容器。
 --no-color 不使用颜色来区分不同的服务的控制台输出。
 --no-deps 不启动服务所链接的容器。
 --force-recreate强制重新创建容器，不能与--no-recreate同时使用。
 --no-recreate如果容器已经存在了，则不重新创建，不能与--force-recreate同时使用。
 --no-build 不自动构建缺失的服务镜像。
 -t,--timeout TIMEOUT停止容器时候的超时(默认为10秒)。
</code></pre><p>其它命令如下：</p>
<table>
  <thead>
      <tr>
          <th>命令</th>
          <th>说明</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>version</td>
          <td>格式为docker-compose version,打印版本信息</td>
      </tr>
      <tr>
          <td>config</td>
          <td>验证Compose格式是否正确，若正确则显示配置，若格式错误显示错误原因。</td>
      </tr>
      <tr>
          <td>exec</td>
          <td>进入指定的容器</td>
      </tr>
      <tr>
          <td>images</td>
          <td>列出Compose文件中包含的镜像</td>
      </tr>
      <tr>
          <td>down</td>
          <td>停止up命令所启动的容器，并移除网络。</td>
      </tr>
      <tr>
          <td>help</td>
          <td>获得一个命令的帮助</td>
      </tr>
      <tr>
          <td>kill</td>
          <td>通过发送SIGKILL信号来强制停止服务容器</td>
      </tr>
      <tr>
          <td>pause</td>
          <td>格式为docker-compose pause [SERVICE&hellip;],暂停一个服务容器。</td>
      </tr>
      <tr>
          <td>push</td>
          <td>推送服务依赖的镜像到Docker镜像仓库</td>
      </tr>
      <tr>
          <td>start</td>
          <td>格式为docker-compose start[SERVICE&hellip;],启动已经存在的服务容器。</td>
      </tr>
      <tr>
          <td>stop</td>
          <td>停止已经存在的服务容器。</td>
      </tr>
      <tr>
          <td>top</td>
          <td>查看各个服务容器内运行的进程</td>
      </tr>
      <tr>
          <td>unpause</td>
          <td>格式为docker-compose unpause [SERVICE&hellip;],恢复处于暂停状态中的服务。</td>
      </tr>
  </tbody>
</table>
<h2 id="compose模板文件">Compose模板文件</h2>
<p>模板文件是使用Compose的核心，涉及到的指令关键字也比较多，大部分指令跟docker run 相关参数的含义都类似。</p>
<p>默认的模板文件名称为docker-compose.yml,格式为YAML格式。</p>
<p>注意每个服务都必须通过image指令指定镜像或build指令(需要Dockerfile)等来自动构建生成镜像。</p>
<p>如果使用build指令,在Dockerfile中设置的选项(例如:CMD,EXPOSE,VOLUME,ENV等)将会自动被获取，无需在docker-compose.yml中再次设置。下面介绍常用指令的用法。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yml" data-lang="yml"><span style="display:flex;"><span><span style="color:#f92672">version</span>: <span style="color:#e6db74">&#39;3&#39;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">services</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">webapp</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">build</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">context</span>: <span style="color:#ae81ff">./dir</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">dockerfile</span>: <span style="color:#ae81ff">Dockerfile-alternate</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">args</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">buildno</span>: <span style="color:#ae81ff">1</span>
</span></span></code></pre></div><p>build</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>指令Dockerfile所在文件夹的路径<span style="color:#f92672">(</span>可以是绝对路径,或者相对docker-compose.yml文件的路径<span style="color:#f92672">)</span>。
</span></span><span style="display:flex;"><span>Compose将会利用它自动构建这个镜像,然后使用这个镜像。
</span></span><span style="display:flex;"><span>使用context指令指定Dockerfile所在文件夹的路径
</span></span><span style="display:flex;"><span>使用dockerfile指令指定Dockerfile文件名
</span></span><span style="display:flex;"><span>使用arg指令指定构建镜像时的变量
</span></span></code></pre></div><p>command</p>
<p>覆盖容器启动后默认执行的命令。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>command:echo <span style="color:#e6db74">&#34;hello world&#34;</span>
</span></span></code></pre></div><p>Container_name</p>
<p>指令容器名称。默认将会使用项目名称_服务名称_序号这样的格式。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>container_name:docker-web-container
</span></span></code></pre></div><p>configs</p>
<p>仅用于Swarm mode, 详细内容后页面swarm mode会进到。</p>
<p>deploy</p>
<p>仅用于Swarm mode,详细内容后面swarm mode会进到。</p>
<p>devices</p>
<p>指定设备映射关系。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>devices:
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;/dev/ttyUSB1:/ttyUSB0&#34;</span>
</span></span></code></pre></div><p>depends_on</p>
<p>解决容器的依赖、启动先后的问题。</p>
<p>dns</p>
<p>自定义DNS服务器。可以是一个值，也可以是一个列表。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>dns: 8.8.8.8
</span></span><span style="display:flex;"><span>dns:
</span></span><span style="display:flex;"><span>  - 8.8.8.8
</span></span><span style="display:flex;"><span>  - 114.114.114.114
</span></span></code></pre></div><p>environment</p>
<p>设置环境变量。你可以使用数组或字典两种格式。
只给定名称的变量会自动获取运行Compose主机上对应变量的值，可以用来防止泄露不必要的数据。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>environment:
</span></span><span style="display:flex;"><span>  RACK_ENV: development
</span></span><span style="display:flex;"><span>  SESSION_SECRET:
</span></span><span style="display:flex;"><span>environment:
</span></span><span style="display:flex;"><span>  - RACK_ENV<span style="color:#f92672">=</span>development
</span></span><span style="display:flex;"><span>  - SESSION_SECRET  
</span></span></code></pre></div><p>expose</p>
<p>显露端口，但不映射到宿主机，只被连接的服务访问。仅可以指定内部端口为参数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>expose:
</span></span><span style="display:flex;"><span>  - <span style="color:#e6db74">&#34;3000&#34;</span>
</span></span><span style="display:flex;"><span>  - <span style="color:#e6db74">&#34;8000&#34;</span>
</span></span></code></pre></div><p>extra_hosts</p>
<p>类似Docker中的&ndash;add-host参数，指定额外的host名称映射信息。
会在启动后的服务容器中/etc/hosts文件中添加一条条目。8.8.8.8 googledns</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>extra_hosts:
</span></span><span style="display:flex;"><span>  - <span style="color:#e6db74">&#34;googledns:8.8.8.8&#34;</span>
</span></span></code></pre></div><p>healthcheck</p>
<p>通过命令容器是否健康运行。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>healthcheck:
</span></span><span style="display:flex;"><span>   test:<span style="color:#f92672">[</span><span style="color:#e6db74">&#34;CMD&#34;</span>,<span style="color:#e6db74">&#34;curl&#34;</span>,<span style="color:#e6db74">&#34;-f&#34;</span>,<span style="color:#e6db74">&#34;http://localhost&#34;</span><span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>   interval:1m30s
</span></span><span style="display:flex;"><span>   timeout:10s
</span></span><span style="display:flex;"><span>   retries:3
</span></span></code></pre></div><p>image</p>
<p>指定为镜像名称或镜像ID。如果镜像在本地不存在，Compose将会尝试拉去这个镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>image:session-web:latest
</span></span></code></pre></div><p>lables</p>
<p>为容器添加Docker元数据(metadata)信息。例如可以为容器添加辅助说明信息。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>labels:
</span></span><span style="display:flex;"><span>  com.study.department:<span style="color:#e6db74">&#34;devops department&#34;</span>
</span></span><span style="display:flex;"><span>  com.study.release:<span style="color:#e6db74">&#34;v1.0&#34;</span>
</span></span></code></pre></div><p>links</p>
<p>连接到其他容器。注意：不推荐使用该指令。
应该使用docker network,建立网络,而docker run &ndash;network来连接特定网络。或者使用version:&lsquo;2&rsquo;和更高版本的docker-compose.yml直接定义自定义网络并使用。</p>
<p>network_mode</p>
<p>设置网络模式。使用和docker run的&ndash;network参数一样的值。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>network_mode:<span style="color:#e6db74">&#34;bridge&#34;</span>
</span></span><span style="display:flex;"><span>network_mode:<span style="color:#e6db74">&#34;host&#34;</span>
</span></span><span style="display:flex;"><span>network_mode:<span style="color:#e6db74">&#34;none&#34;</span>
</span></span></code></pre></div><p>networks</p>
<p>配置容器连接的网络。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>version:<span style="color:#e6db74">&#34;3&#34;</span>
</span></span><span style="display:flex;"><span>services:
</span></span><span style="display:flex;"><span>  some-service:
</span></span><span style="display:flex;"><span>    networks:
</span></span><span style="display:flex;"><span>      - some-network
</span></span><span style="display:flex;"><span>networks:
</span></span><span style="display:flex;"><span>  some-network:      
</span></span></code></pre></div><p>ports</p>
<p>暴露端口信息。使用宿主端口：容器端口(HOST:CONTAINER)格式，或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ports:
</span></span><span style="display:flex;"><span>  - <span style="color:#e6db74">&#34;3000&#34;</span>
</span></span><span style="display:flex;"><span>  - <span style="color:#e6db74">&#34;8000:8000&#34;</span>
</span></span></code></pre></div><p>volumes</p>
<p>数据卷所挂载路径设置，可以设置宿主机路径，同时支持相关路径。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>volumes:
</span></span><span style="display:flex;"><span>  - /var/lib/mysql
</span></span><span style="display:flex;"><span>  - cache/:/tmp/cache
</span></span><span style="display:flex;"><span>  - ~/configs:/etc/configs/:ro
</span></span></code></pre></div><p>ulimits</p>
<p>指定容器的ulimits限制值。
例如，指定最大进程数为65535,指定文件句柄数为20000(软限制,应用可以随时修改,不能超过硬限制)和40000(系统硬限制，只能root用户提高)。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ulimits:
</span></span><span style="display:flex;"><span>  nproc: <span style="color:#ae81ff">65535</span>
</span></span><span style="display:flex;"><span>  nofile:
</span></span><span style="display:flex;"><span>    soft: <span style="color:#ae81ff">20000</span>
</span></span><span style="display:flex;"><span>    hard: <span style="color:#ae81ff">40000</span>
</span></span></code></pre></div><p>此外，还有包括domainname,entrypoint,hostname,ipc,mac_address,privileged,read_only,shm_size,restart,stdin_open,tty,user,working_dir等指令，基本跟docker run中对应参数的功能相同。</p>
<p>指定服务容器启动后执行的入口文件</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>entrypoint: /code/entrypoint.sh
</span></span></code></pre></div><p>指定容器中运行应用的用户名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>user:nginx
</span></span></code></pre></div><p>指定容器中工作目录</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>working_dir: /code
</span></span></code></pre></div><p>指定容器中搜索域名、主机名、mac地址等</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>domainname:your_website.com
</span></span><span style="display:flex;"><span>hostname:test
</span></span><span style="display:flex;"><span>mac_address:08-00-27-00-0C-0A
</span></span></code></pre></div><p>允许容器中运行一些特权命令</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>privileged:true
</span></span></code></pre></div><p>指定容器退出后的重启策略为始终重启。在生产环境中推荐配置为always或者unless-stopped</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>restart:alwarys
</span></span></code></pre></div><p>以只读模式挂载容器的root文件系统，意味着不能对容器内容进行修改</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>read_only:true
</span></span></code></pre></div><p>打开标准输入，可以接受外部输入</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>stdin_open:true
</span></span></code></pre></div><p>模拟一个伪终端</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>tty:true
</span></span></code></pre></div><p>Compose模板文件支持动态读取主机的系统环境变量和当前目录下的.env文件中的变量。例如，下面的Compose文件将从运行它的环境中读取变量${MONGO_VERSION}的值，并写入执行的指令中。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>version: <span style="color:#e6db74">&#34;3&#34;</span>
</span></span><span style="display:flex;"><span>services:
</span></span><span style="display:flex;"><span>  db:
</span></span><span style="display:flex;"><span>    image: <span style="color:#e6db74">&#34;mongo:</span><span style="color:#e6db74">${</span>MONGO_VERSION<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>
</span></span></code></pre></div><p>如果执行MONGO_VERSION=3.2 docker-compose up则会启动一个mongo:3.2镜像的容器。若当前目录存在.env文件，执行docker-compose命令时将从该文件中读取变量。</p>

        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
